/*
 * DataBase.h
 *
 *  Created on: 24.03.2009
 *      Author: Konan Andrej
 */

#ifndef DATABASE_H_
#define DATABASE_H_

#include <QDataStream>
#include <QString>
#include <QVector>
#include <QObject>
#include <QFile>
#include <QMap>

#include "Defs.h"
#include "Debug.h"
#include "ItemType.h"

struct DataBaseInfo
{
	int ItemsNumber;
	int RecordsNumber;
        int VisibleNumber;              //first VisibleNumber of RecordsNumber are number of visible fields
	int KeyField;
	QString DBName;
	QVector<QString> ColumnHeaders;
        QVector<int> ColumnWidth;       //width of visible columns
};

class DataBase : public QObject
{
    Q_OBJECT
private:
	QMap<QString, ItemType> Items;

	DataBaseInfo Info;

	int ReadInfoFile( const QString& FileName );
	int WriteInfoFile( const QString& FileName );

        void CheckColumnWidth( const ItemType& item);

signals:
        void ItemEdited();
        void ItemAdded();
        void ItemRemoved();

public:
        DataBase();
        DataBase( const DataBaseInfo& Info );
        DataBase( const DataBase& DB );
        ~DataBase();

        DataBase& operator =( const DataBase& DB );

        int SaveDatabaseToFile();
	int SaveDatabaseToFile( const QString& FileName );
	int LoadDatabaseFromFile ( const QString& FileName );

	int GetItem( const QString& Key, ItemType& item ) const;
	int GetItem( const int index, ItemType& item ) const;
        int GetItemIndex( const QString &Key ) const;

        QString getName() const { return Info.DBName; }
        void setColumnHeader( int i, const QString &name ) { Info.ColumnHeaders[i] = name; }

	int InsertItem( const ItemType& item );
	int RemoveItem( const ItemType& item );
	int EditItem( const ItemType& item );

	int GetItemsCount() const { return Info.ItemsNumber; }
        int GetKeyField() const { return Info.KeyField; }

	int GetVisibleNumber() const { return Info.VisibleNumber; }
        int GetFieldNumber() const { return Info.RecordsNumber; }

	QString GetColumnHeader( int i ) const { return Info.ColumnHeaders[i]; }
        void GetColumnHeaderVector( QVector<QString>& headers );

        int GetColumnWidth( int i ) const { return Info.ColumnWidth[i]; }


	//int SetStoreStep( int step );
};

#endif /* DATABASE_H_ */
